import 'package:enterprise_v6_app/app/components/custom_refresh.dart';
import 'package:enterprise_v6_app/app/components/dialog/filter_dialog/filter_utils.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/http/http.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/entities/application/emergency/exercise_plan_entity.dart';
import 'package:enterprise_v6_app/app/data/entities/application/filter/filter_bean.dart';
import 'package:enterprise_v6_app/app/data/entities/application/filter/filter_entities_bean.dart';
import 'package:enterprise_v6_app/app/data/entities/application/filter/filter_field_parameter_bean.dart';
import 'package:enterprise_v6_app/router/app_pages.dart';

class DrillPlanController extends GetxController {
  /// 筛选右侧栏控制Key
  final popRightScaffoldKey = GlobalKey<ScaffoldState>();

  /// 搜索使用key
  final GlobalKey<CustomEasyRefreshListState> globalKey = GlobalKey();

  /// 搜索字符串
  String? search;

  /// 筛选列表
  List<FilterBean> allFilterList = [];

  /// 筛选字段数据
  List<FilterFieldParameterBean> fieldParameterList = [];

  /// 筛选字段请求数据
  Map<String, dynamic> filterMap = {};

  @override
  void onInit() {
    super.onInit();
    initData();
  }

  /// 初始化数据
  Future<void> initData() async {
    // 添加筛选列表
    fieldParameterList = [
      FilterFieldParameterBean(
        screenName: '演练类别',
        parameterName: 'exercisePlanClass',
        dictionaryType: 'yllb_OW2j3KXp',
      ),
      FilterFieldParameterBean(
        screenName: '演练类型',
        parameterName: 'exercisePlanType',
        dictionaryType: 'yllx_URzc6ZRS',
      ),
      FilterFieldParameterBean(
        screenName: '状态',
        parameterName: 'planStatus',
      ),
    ];
    // 获取字典筛选列表
    allFilterList = await FilterUtils.getDictFilterList(fieldParameterList: fieldParameterList);
    // 添加不属于字典的筛选条件
    final List<FilterEntitiesBean> stateList = [];
    // 添加筛选条件
    stateList.add(FilterEntitiesBean(name: '待发起', value: 1));
    stateList.add(FilterEntitiesBean(name: '待审批', value: 2));
    stateList.add(FilterEntitiesBean(name: '审批中', value: 3));
    stateList.add(FilterEntitiesBean(name: '通过', value: 4));
    stateList.add(FilterEntitiesBean(name: '已驳回', value: 5));
    stateList.add(FilterEntitiesBean(name: '待发布', value: 6));
    stateList.add(FilterEntitiesBean(name: '待开始', value: 7));
    stateList.add(FilterEntitiesBean(name: '进行中', value: 8));
    stateList.add(FilterEntitiesBean(name: '已结束', value: 9));
    stateList.add(FilterEntitiesBean(name: '已废止', value: 10));

    allFilterList.add(FilterBean(title: '状态', filterList: stateList));
    update();
  }

  Future<List<ExercisePlanEntity>> getListData(int pageNum, int pageSize) async {
    filterMap.clear();
    final Map<String, dynamic> queryParameters = {
      'pageNum': pageNum,
      'pageSize': pageSize,
      'exercisePlanName': search,
    };
    // 获取筛选处参数
    for (var item in fieldParameterList) {
      if (item.parameterValue != null) {
        filterMap[item.parameterName] = item.parameterValue;
      }
    }
    queryParameters.addAll(filterMap);
    // 获取列表接口
    final res = await HttpUtil()
        .generateGetRequest<List<ExercisePlanEntity>>(
          Interface.getExercisePlanPage,
          queryParameters: queryParameters,
        )
        .execute();
    List<ExercisePlanEntity>? data;
    res.onSuccess((result) {
      if (result.data != null) {
        data = result.data!;
      }
    });
    return data ?? [];
  }

  /// 筛选
  void screen() {
    popRightScaffoldKey.currentState?.openEndDrawer();
  }

  /// 筛选确认
  void screenConfirm(List<FilterBean> value) {
    allFilterList = value;
    // 参数清空
    for (var item in fieldParameterList) {
      item.parameterValue = null;
    }
    for (var i in value) {
      if (i.chosenValue.isEmpty) {
        continue;
      }
      // 获取标题相同的参数实体
      final result = fieldParameterList.firstWhere((data) => data.screenName == i.title);
      // 单选所以选中第一个就可以
      result.parameterValue = i.chosenValue[0].value;
    }
    update();
    refreshPage();
  }

  /// 搜索
  void refreshPage() {
    globalKey.currentState?.onRefresh();
  }

  /// 进入详情
  /// [id]主键id
  /// type:1 演练计划 2演练记录
  void jumpToDetails({String? id}) {
    Get.toNamed(AppRoutes.drillDetails, arguments: {'type': 1, 'id': id});
  }
}
